var MemberRouter = Backbone.Router.extend({
  routes: {
    "register": "register",
    "login": "login",
    "profile": "profile",
    "active" : "active",
    "*path": "profile",
  },

  initialize: function(options) {
    this.on('route', function(route) {
      if (route != 'login' && route != 'register') {
        $('#spinner').show();
      }
    });

    this.on('loading', function() {
      $('#spinner').show();
    });

    this.on('loaded', function(route) {
      if(portRefuse && isWechat) {
        this.navigateTo('active');
        return ;
      }
      window.setTimeout(function() {
        $('#spinner').hide();
        componentHandler.upgradeDom();
      }, 300);

      // page share on wechat
      var url = location.href.replace(/#.*/, '');

    });
  },
  navigateTo: function (route) {
    this.navigate(route, { trigger: true });
  },

  register: function() {
    if (window.currentUser) {
      this.navigateTo('profile');
      return ;
    }
    if(!isWechat) {
      this.navigateTo('login');
      return ;
    }
    if (typeof window.registerUser == 'undefined') {
      window.registerUser = new UserModel({});
    }

    var authRegisterView = new AuthRegisterView({model: window.registerUser});
    $('#ajax-content').html(authRegisterView.render().el);
    this.trigger('loaded');
  },

  login: function() {
    if (window.currentUser) {
      return this.navigateTo('profile');
    }
    if(isWechat) {
      return this.navigateTo('register');
    }
    var user = new UserModel({});
    var authLoginView = new AuthLoginView({model: user});
    $('#ajax-content').html(authLoginView.render().el);
    this.trigger('loaded');
  },
  active: function(){
    if(!portRefuse && isWechat) {
      this.navigateTo('register');
      return ;
    }
    var authActiveView = new AuthActiveView({});
    $('#ajax-content').html(authActiveView.render().el);
    this.trigger('loaded');
  },
  profile: function(option,option2) {
    var option = option || '';
    var option2 = option2 || '';
    var promise = this.getUser();
    var that = this;
      promise.then(function() {
        if(window.currentUser != null) {
          // if(window.currentUser.get('is_contact') == 0){
          //     return that.navigateTo('active');
          // }
        var userProfileView = new UserProfileView({model: window.currentUser});
        $('#ajax-content').empty().append(userProfileView.render().el);
        window.setTimeout(function() {
          that.trigger('loaded');
        }, 500);
        }
      });

  },

  getUser: function () {
    if (!window.currentUser) {
      var users = new UserCollection();
      var that = this;
      //return a promise;
      return users.fetch().then(function (data) {
        if (users.models.length === 0) {

            if(isWechat) {
              that.navigateTo('register');
            } else {
              that.navigateTo('login');
            }

          return ;
        }
        window.currentUser = users.at(0);
      }, function() {
        if(isWechat) {
          that.navigateTo('register');
        } else {
          that.navigateTo('login');
        }
      });
    } else {
      return $.when(window.currentUser);
    }
  },
});